<template>
  <div>
    <el-dialog
      :title="dialogtitle"
      width="1000px"
      class="icon-dialog"
      :visible.sync="show"
      :before-close="closeForm"
      append-to-body
      @open="open"
    >
      <el-form
        ref="form"
        :size="size"
        :model="form"
        :rules="rules"
        :label-width="$store.getters.device !== 'mobile'?'16%':'90px'"
      >
      <el-row>
        <el-col :span="24">
          <el-form-item label="积分规则">
            <div style="margin-bottom: 8px;" v-for="(item, index) in form.MemberAccountObtain" :key="index">
                <el-radio-group v-model="item.calculate_method">
                  <el-radio :label="0">百分比</el-radio>
                  <el-radio :label="1">固定积分</el-radio>
                </el-radio-group>
                <el-select v-model="item.ptype" style="width:180px;margin-left: 10px;margin-right: 3px;" filterable clearable placeholder="请选择">
                  <el-option v-for="(oitem,i) in form.getobtainPtypearray" :key="i" :label="oitem.key" :value="oitem.val.toString()" />
                </el-select>
                <el-input v-model="item.price" style="width:180px;margin-left: 10px;margin-right: 3px;" auto-complete="off" clearable
                  placeholder="积分">
                  <template v-if="item.calculate_method=='0'" slot="append">%</template>
                  <template v-if="item.calculate_method=='1'" slot="append">分</template>
                </el-input>
                <el-button @click="addItem()">+</el-button>
                <el-button @click="deleteItem(index)">–</el-button>

            </div>
            <div class="help-block">

            </div>
          </el-form-item>
        </el-col>
      </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="loading" type="primary" @click="submit">
          <span v-if="!loading">保 存</span>
          <span v-else>提 交 中...</span>
        </el-button>
        <el-button @click="closeForm">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'memberaccounttypeobtain',
  components: {},
  props: {
    show: {
      type: Boolean,
      default: false
    },
    opentype: {
      type: String,
      default: 'add'
    },
    size: {
      type: String,
      default: 'small'
    },
    info: {
      type: Object
    }
  },
  data() {
    return {
      lang: {},
      dialogtitle: '',
      form: {
        title: '',
        status: 1
      },
      loading: false,
      rules: {
        name: [{
          required: true,
          message: '名称不能为空',
          trigger: 'blur'
        }]
      }
    }
  },
  watch: {
    show(val) {
      if (val) {
      }
    }
  },
  mounted() {
    this.$api.post('/Lang/getlang').then(res => {
      this.lang = res.data
    })
  },
  methods: {
    open() {
      if (this.opentype == 'obtain') {
        this.dialogtitle = this.info.name + '获取积分规则'
        this.form = this.info
        console.log(this.form);
      } else {
        this.dialogtitle = '添加'
        this.form = {
          goods_ids: 0,
          goods: [],
          expire_type: 10,
          sort: 100,
          status: 1
        }
        // console.log(this.form);
      }
    },
    submit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          this.$api.post('/memberaccounttype/update', this.form).then(res => {
            this.$message({
              message: res.msg,
              type: 'success'
            })
            this.$emit('refesh_list')
            this.closeForm()
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },
    closeForm() {
      this.$emit('update:show', false)
      this.loading = false
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
    },
    normalizer(data) {
      if (data.children && !data.children.length) {
        delete data.children
      }
      return {
        id: data.val,
        label: data.key,
        children: data.children
      }
    },
    addItem() {
      let that = this;
      that.form.MemberAccountObtain.push({
        calculate_method: 0,
        ptype: '',
        return_percent: ''
      });
    },
    deleteItem(i) {
      if (this.form.MemberAccountObtain.length <= 1) {
        return false;
      }
      this.form.MemberAccountObtain.splice(i, 1);
    }
  }
}
</script>
<style scoped>
  .goods_pic {
    width: 100px;
    vertical-align: middle;
  }
</style>
